﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenMenu
{
    public partial class ProgressForm : Form
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

        public event Action CancelClick;

        public void ShowMessage(string msg, bool canCancel)
        {
            if (InvokeRequired)
            {
                Invoke(new Action(() =>
                    {
                        _lblMessage.Text = msg;
                        _btnCancel.Visible = canCancel;
                    }));
            }
            else
            {
                _lblMessage.Text = msg;
                _btnCancel.Visible = canCancel;
            }
        }

        public void CloseForm()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(Close));
            }
            else
            {
                Close();
            }
        }

        public void ShowError(Exception exception)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<Exception>(DisplayError), exception);
            }
            else
            {
                DisplayError(exception);
            }
        }

        private void DisplayError(Exception obj)
        {
            _lblMessage.Text = "Action runs in error! " + obj.Message;
            _lblMessage.ForeColor = Color.Red;
            _btnOK.Visible = true;
            _btnCancel.Visible = false;
        }

        private void _btnCancel_Click(object sender, EventArgs e)
        {
            if (CancelClick != null)
            {
                CancelClick();
            }
        }


    }
}
